import Picker from '../mod/picker';

export default {
  /**
   * 设置数字几位数显示；例console.log(fix(5, 2));输出结果为05
   * @param num
   * @param length
   * @returns {string}
   */
  fix: function (num, length) {
    return ('' + num).length < length ? ((new Array(length + 1)).join('0')
        + num).slice(-length) : '' + num;
  },
  // getDate: function (dateTime) {
  //   dateTime = dateTime.trim();
  //   var dateTimeArr = dateTime.split(' ');
  //   var date = (dateTimeArr[0].length == 10) ? dateTimeArr[0] : '';
  //   return date;
  // },
  /**
   * 获取当前系统时间并按照一定格式返回
   * @returns {string} format{yyyy-MM-dd HH:mm:ss}
   */
  getCurDatetime: function () {
    var end = new Date();
    var endyear = this.fix(end.getFullYear(), 4);
    var endmonth = this.fix(parseInt(end.getMonth() + 1), 2);
    var endday = this.fix(end.getDate(), 2);
    var endhours = this.fix(end.getHours(), 2);
    var endminutes = this.fix(end.getMinutes(), 2);
    var endseconds = this.fix(end.getSeconds(), 2);
    var curtime = endyear + '-' + endmonth + '-' + endday + ' ' + endhours + ':'
        + endminutes + ':' + endseconds;
    return curtime;
  },
  /**
   * 根据一定类型时间获取时间格式返回
   * @param _date
   * @returns {string} format{yyyy-MM-dd HH:mm:ss}
   */
  getDatetimeBy(_date) {
    let _this = this;
    let end = null;
    if (typeof _date == "object") {
      end = _date;
    } else {
      end = new Date(_date);
    }
    var endyear = this.fix(end.getFullYear(), 4);
    var endmonth = this.fix(parseInt(end.getMonth() + 1), 2);
    var endday = this.fix(end.getDate(), 2);
    var endhours = this.fix(end.getHours(), 2);
    var endminutes = this.fix(end.getMinutes(), 2);
    var endseconds = this.fix(end.getSeconds(), 2);
    var curtime = endyear + '-' + endmonth + '-' + endday + ' ' + endhours + ':'
        + endminutes + ':' + endseconds;
    return curtime;
  },
  /**
   * 时间戳转换为 yyyy-MM-dd hh:mm:ss
   * @param timestamp
   * @returns {string}
   */
  timestampToFormatDatetime: function (timestamp) {
    var end = new Date(timestamp);
    var endyear = this.fix(end.getFullYear(), 4);
    var endmonth = this.fix(parseInt(end.getMonth() + 1), 2);
    var endday = this.fix(end.getDate(), 2);
    var endhours = this.fix(end.getHours(), 2);
    var endminutes = this.fix(end.getMinutes(), 2);
    var endseconds = this.fix(end.getSeconds(), 2);
    var curtime = endyear + '-' + endmonth + '-' + endday + ' ' + endhours + ':'
        + endminutes + ':' + endseconds;
    return curtime;
  },
  /**
   * 时间戳转换，当天返回 hh:mm，一天以前返回 n天前
   * @param timestamp yyyy-MM-dd hh:mm:ss
   * @returns {string}
   */
  toChatFormatTimeDiff: function (timestamp) {
    let _this = this;
    // 获取时间戳对应的日期时间，格式 yyyy-MM-dd hh:mm:ss
    var lasttime = this.timestampToFormatDatetime(timestamp);
    // 获取当前系统日期时间，格式 yyyy-MM-dd hh:mm:ss
    var curtime = this.getCurDatetime();

    var lastDate = new Date(lasttime.substr(0, 4), lasttime.substr(5, 2) - 1,
        lasttime.substr(8, 2), lasttime.substr(11, 2), lasttime.substr(14, 2),
        lasttime.substr(17, 2));
    var curDate = new Date(curtime.substr(0, 4), curtime.substr(5, 2) - 1,
        curtime.substr(8, 2), curtime.substr(11, 2), curtime.substr(14, 2),
        curtime.substr(17, 2));

    var dateDifference = curDate.getTime() - lastDate.getTime();//时间差的毫秒数
    var days = Math.floor(dateDifference / (24 * 3600 * 1000))//计算出相差天数
    //计算出小时数
    var leave1 = dateDifference % (24 * 3600 * 1000);    //计算天数后剩余的毫秒数
    var hours = Math.floor(leave1 / (3600 * 1000));
    //计算相差分钟数
    var leave2 = leave1 % (3600 * 1000);        //计算小时数后剩余的毫秒数
    var minutes = Math.floor(leave2 / (60 * 1000));
    //计算相差秒数
    var leave3 = leave2 % (60 * 1000);      //计算分钟数后剩余的毫秒数
    var seconds = Math.round(leave3 / 1000);
    var retDate = days + "天 " + hours + "小时 " + minutes + "分钟 " + seconds + "秒";

    if (days > 0) {
      if (days >= 7) {
        retDate = _this.getDatetimeBy(lastDate).substr(0, 10);
      } else {
        retDate = days + '天前'
      }
    } else {
      if (lasttime.substr(0, 10) != curtime.substr(0, 10)) {
        retDate = '昨天 ' + lasttime.substr(11, 5);
      } else {
        retDate = lasttime.substr(11, 5);
      }
    }
    // console.log("result date : " + retDate);
    return retDate;
  },
  /**
   * 根据开始时间与时间差，计算几天前/几天后的具体时间
   * @param startDate : 格式为 yyyy-MM-dd hh:mm:ss
   * @param difference : 格式为数值，可为负，为负时，计算该开始时间之前日期
   * @returns {string} : 格式为 yyyy-MM-dd hh:mm:ss
   */
  getAnotherDatetimeBy: function (startDate, difference) {
    var dateObj = new Date(startDate);
    dateObj.setDate(dateObj.getDate() + difference);
    var year = this.fix(dateObj.getFullYear(), 4);
    var month = this.fix(parseInt(dateObj.getMonth() + 1), 2);
    var day = this.fix(dateObj.getDate(), 2);
    var hours = this.fix(dateObj.getHours(), 2);
    var minutes = this.fix(dateObj.getMinutes(), 2);
    var seconds = this.fix(dateObj.getSeconds(), 2);

    var resTime = year + '-' + month + '-' + day + ' ' + hours + ':'
        + minutes + ':' + seconds;
    return resTime;
  },
  computeDiffBy: function (startDate, type, diff) {
    let _this = this;
    let startDateObj = new Date(startDate);
    let getDatetimeStr = function (dateObj) {
      var year = _this.fix(dateObj.getFullYear(), 4);
      var month = _this.fix(parseInt(dateObj.getMonth() + 1), 2);
      var day = _this.fix(dateObj.getDate(), 2);
      var hours = _this.fix(dateObj.getHours(), 2);
      var minutes = _this.fix(dateObj.getMinutes(), 2);
      var seconds = _this.fix(dateObj.getSeconds(), 2);

      var resTime = year + '-' + month + '-' + day + ' ' + hours + ':'
          + minutes + ':' + seconds;
      return resTime;
    };
    let endDateStr = '';
    switch (type) {
      case 'minute':
        startDateObj.setMinutes(startDateObj.getMinutes() + diff);
        endDateStr = getDatetimeStr(startDateObj);
        break;
      case 'hour':
        startDateObj.setHours(startDateObj.getHours() + diff);
        endDateStr = getDatetimeStr(startDateObj);
        break;
      case 'day':
        startDateObj.setDate(startDateObj.getDate() + diff);
        endDateStr = getDatetimeStr(startDateObj);
        break;
      case 'week':
        diff = diff * 7;
        startDateObj.setDate(startDateObj.getDate() + diff);
        endDateStr = getDatetimeStr(startDateObj);
        break;
    }
    return endDateStr;
  },
  /**
   * 将一段时间的字符串转换为一定格式为: yyyy-MM-dd hh:mm:ss
   * @param dateTimeStr
   * @returns {string}
   */
  getFormatDateTime: function (dateTimeStr) {
    var end = new Date(dateTimeStr);
    var endyear = this.fix(end.getFullYear(), 4);
    var endmonth = this.fix(parseInt(end.getMonth() + 1), 2);
    var endday = this.fix(end.getDate(), 2);
    var endhours = this.fix(end.getHours(), 2);
    var endminutes = this.fix(end.getMinutes(), 2);
    var endseconds = this.fix(end.getSeconds(), 2);
    var curtime = endyear + '-' + endmonth + '-' + endday + ' ' + endhours + ':'
        + endminutes + ':' + endseconds;
    return curtime;
  },
  /**
   * 获取某天的年月日
   * @param date
   * @returns {string} 格式：yyyy-mm-dd
   */
  formatDate: function (date) {
    let endyear = this.fix(date.getFullYear(), 4);
    let endmonth = this.fix(parseInt(date.getMonth() + 1), 2);
    let endday = this.fix(date.getDate(), 2);
    return (endyear + "-" + endmonth + "-" + endday);
  },
  /**
   * 获取某月的天数
   * @param myMonth
   * @returns {number}
   */
  getMonthDays: function (myMonth) {
    var now = new Date(); // 当前日期
    var nowYear = now.getYear(); //当前年
    nowYear += (nowYear < 2000) ? 1900 : 0; //
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  },
  /**
   * 获取某年某月的天数
   * @param myYear
   * @param myMonth
   * @returns {number}
   */
  getMonthDaysByYear: function (myYear, myMonth) {
    myMonth = myMonth - 1;
    var monthStartDate = new Date(myYear, myMonth, 1);
    var monthEndDate = new Date(myYear, myMonth + 1, 1);
    // console.log(myMonth + '月的初始日期：', monthStartDate);
    // console.log((myMonth + 1) + '月的初始日期: ', monthEndDate);
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  },
  /**
   * 获取当前季度的开始月份
   * @returns {number}
   */
  getQuarterStartMonth: function () {
    var now = new Date(); // 当前日期
    var nowMonth = now.getMonth(); //当前月
    var quarterStartMonth = 0;
    if (nowMonth < 3) {
      quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
      quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
      quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
      quarterStartMonth = 9;
    }
    return quarterStartMonth;
  },
  /**
   * 获取开始日期与结束日期
   * @param period ： week本周，month本月，quarter本季度
   */
  getStartEndDate: function (period) {
    const _this = this;
    var now = new Date(); // 当前日期
    var nowDayOfWeek = now.getDay(); // 今天是本周第几天
    var nowDay = now.getDate(); //当前日
    var nowMonth = now.getMonth(); //当前月
    var nowYear = now.getYear(); //当前年
    nowYear += (nowYear < 2000) ? 1900 : 0; //

    var lastMonthDate = new Date(); //上月日期
    lastMonthDate.setDate(1);
    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
    var lastYear = lastMonthDate.getYear();
    var lastMonth = lastMonthDate.getMonth();

    var startDate = '';
    var endDate = '';
    switch (period) {
      case 'week':
        // var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
        var weekStartDay = parseInt(parseInt(nowDay - nowDayOfWeek) + 1);
        var weekEndDay = parseInt(parseInt(nowDay - nowDayOfWeek) + 7);
        var weekStartDate = new Date(nowYear, nowMonth, weekStartDay);
        startDate = _this.formatDate(weekStartDate);
        // var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
        var weekEndDate = new Date(nowYear, nowMonth, weekEndDay);
        endDate = _this.formatDate(weekEndDate);
        break;
      case 'month':
        var monthStartDate = new Date(nowYear, nowMonth, 1);
        startDate = _this.formatDate(monthStartDate);
        var monthEndDate = new Date(nowYear, nowMonth,
            _this.getMonthDays(nowMonth));
        endDate = _this.formatDate(monthEndDate);
        break;
      case 'quarter':
        var quarterStartDate = new Date(nowYear, _this.getQuarterStartMonth(),
            1);
        startDate = _this.formatDate(quarterStartDate);
        var quarterEndMonth = _this.getQuarterStartMonth() + 2;
        var quarterStartDate1 = new Date(nowYear, quarterEndMonth,
            _this.getMonthDays(quarterEndMonth));
        endDate = _this.formatDate(quarterStartDate1);
        break;
      case 'year':
        var yearStartDate = new Date(nowYear, 0, 1);
        startDate = _this.formatDate(yearStartDate);
        var yearEndDate = new Date(nowYear, 11, _this.getMonthDays(11));
        endDate = _this.formatDate(yearEndDate);
        break;
      default:
        break;
    }

    return {
      start: startDate,
      end: endDate
    };
  },
  /**
   * 选择日期控件，格式可为yyyy-mm-dd/yyyy.mm.dd/yyyy年mm月dd日
   * @param context：framework7 的 this 对象
   * @param params：inputEl、currentDate、opened、closed、dateFormat
   */
  selectDateInMondal: function (context, params) {
    const _this = this;
    var inputElement = params.inputEl ? params.inputEl : '';
    var currentDateStr = params.currentDate ? params.currentDate : '';
    var openedHandler = params.opened ? params.opened : null;
    var closedHandler = params.closed ? params.closed : null;
    var dateFormat = params.dateFormat ? params.dateFormat : 'yyyy-mm-dd';

    var formatSeparator = '-';
    var isSeparatorCount = 0;
    for (var i in currentDateStr) {
      var item = currentDateStr[i];
      var itemType = (typeof item);
      if (itemType != "number") {
        isSeparatorCount += 1;
        if (isSeparatorCount <= 1) {
          if (item == '年') {
            formatSeparator = '汉字';
          } else {
            // if (item == '-' || item == '.')
            formatSeparator = item;
          }
          break;
        }
      }
    }

    var _initializeCalendarVal = function (calendar, curDateString) {
      var initDate = null;
      var initDateArr = [];
      if (curDateString != '') {
        initDate = new Date(curDateString);
      } else {
        return false;
        // initDate = new Date();
      }
      initDateArr.push(initDate);
      calendar.setValue(initDateArr);
    };

    var calendarObj = context.$f7.calendar.create({
      inputEl: inputElement,
      openIn: 'customModal',
      header: true,
      footer: true,
      headerPlaceholder: '选择时间',
      toolbarCloseText: '确定',
      dateFormat: dateFormat,
      monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月',
        '十一月', '十二月'],
      dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
      formatValue: function (values) {
        var year = _this.fix(values[0].getFullYear(), 4);
        var month = _this.fix(parseInt(values[0].getMonth() + 1), 2);
        var day = _this.fix(values[0].getDate(), 2);

        var resVal = '';
        if (formatSeparator != '汉字') {
          resVal = year + formatSeparator + month + formatSeparator + day;
        } else {
          resVal = year + '年' + month + '月' + day + '日';
        }
        return resVal;
      },
      on: {
        init: function (calendar) {
          _initializeCalendarVal(calendar, currentDateStr);
        },
        opened: function (calendar) {
          if (openedHandler != null) {
            openedHandler(calendar);
          } else {
            _initializeCalendarVal(calendar, currentDateStr);
          }
          Picker.show({el: inputElement});
        },
        closed: function (calendar) {
          if (closedHandler != null) {
            var resultDate = calendar.getValue();
            var resultFormatDate = '';
            if (resultDate.length != 0) {
              var year = _this.fix(resultDate[0].getFullYear(), 4);
              var month = _this.fix(parseInt(resultDate[0].getMonth() + 1), 2);
              var day = _this.fix(resultDate[0].getDate(), 2);

              if (formatSeparator != '汉字') {
                resultFormatDate = year + formatSeparator + month
                    + formatSeparator + day;
              } else {
                resultFormatDate = year + '年' + month + '月' + day + '日';
              }
            }
            if (closedHandler != null) {
              closedHandler(calendar, resultFormatDate);
            }
          }
          Picker.dismiss();
        }
      },
    });
    return calendarObj;
  },
  /**
   * 选择年月日时分控件，格式为yyyy-MM-dd hh:mm
   * @param context：framework7 的 this 对象
   * @param params：inputEl、currentDate、opened、closed
   */
  createDateTimeInPicker: function (context, params) {
    const _this = this;
    var inputElement = params.inputEl ? params.inputEl : '';
    var currentDateStr = params.currentDate ? params.currentDate : '';
    var openedHandler = params.opened ? params.opened : null;
    var closedHandler = params.closed ? params.closed : null;

    var calendarObj = context.$f7.picker.create({
      inputEl: inputElement,
      // toolbar: true,
      rotateEffect: true,
      toolbarCloseText: '确定',
      closeByOutsideClick: false,
      // value: initValue,
      cols: [
        // Years
        {
          values: (function () {
            var arr = [];
            for (var i = 1950; i <= 2030; i++) {
              arr.push(i);
            }
            return arr;
          })(),
        },
        // Months
        {
          values: ('0 1 2 3 4 5 6 7 8 9 10 11').split(' '),
          displayValues: ('01月 02月 03月 04月 05月 06月 07月 08月 09月 10月 11月 12月').split(
              ' '),
          textAlign: 'left',
          onChange: function (picker, val, displayVal) {
            var Month = parseInt(val) + 1;
            var val_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
              17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
            var displayValues_1 = ('1日 2日 3日 4日 5日 6日 7日 8日 9日 10日 11日 12日 13日 14日 15日 16日 17日 18日 19日 20日 21日 22日 23日 24日 25日 26日 27日 28日 29日 30日 31日').split(
                ' ');
            var val_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
              17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
            var displayValues_2 = ('1日 2日 3日 4日 5日 6日 7日 8日 9日 10日 11日 12日 13日 14日 15日 16日 17日 18日 19日 20日 21日 22日 23日 24日 25日 26日 27日 28日 29日 30日').split(
                ' ');
            var val = [];
            var displayValues = [];
            if ([1, 3, 5, 7, 8, 10, 12].indexOf(Month) > -1) {
              val = val_1;
              displayValues = displayValues_1;
            } else {
              val = val_2;
              displayValues = displayValues_2;
            }
            if (picker.cols[2].replaceValues) {
              picker.cols[2].replaceValues(val, displayValues);
            }
          }
        },
        // Days
        {
          values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
            18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
          displayValues: ('1日 2日 3日 4日 5日 6日 7日 8日 9日 10日 11日 12日 13日 14日 15日 16日 17日 18日 19日 20日 21日 22日 23日 24日 25日 26日 27日 28日 29日 30日 31日').split(
              ' '),
        },
        // Space divider
        {
          divider: true,
          content: '  '
        },
        // Hours
        {
          values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
            18, 19, 20, 21, 22, 23],
          displayValues: ('00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23').split(
              ' ')
        },
        // Divider
        {
          divider: true,
          content: ':'
        },
        // Minutes
        {
          values: (function () {
            var arr = [];
            for (var i = 0; i <= 59; i++) {
              arr.push(i < 10 ? '0' + i : i);
            }
            return arr;
          })(),
        }
      ],
      formatValue: function (values, values2, displayValues) {
        var month = _this.fix(parseInt(values[1]) + 1, 2);
        var day = _this.fix(values[2], 2);
        var Hours = _this.fix(values[3], 2);
        var Minutes = _this.fix(values[4], 2);
        return values[0] + '-' + month + '-' + day + ' ' + Hours + ':'
            + Minutes;
      },
      on: {
        init: function (calendar) {
          _this.initDateTimeInPicker(calendar, currentDateStr);
        },
        opened(calendar) {
          Picker.show({el: inputElement});
        },
        closed: function (calendar) {
          var resultDate = calendar.getValue();
          var resultFormatDate = '';
          if (resultDate.length != 0) {
            // console.log(resultDate);
            var year = _this.fix(resultDate[0], 4);
            var month = _this.fix(parseInt(resultDate[1]) + 1, 2);
            var day = _this.fix(resultDate[2], 2);
            var hour = _this.fix(resultDate[3], 2);
            var minute = _this.fix(resultDate[4], 2);
            resultFormatDate = year + '-' + month + '-' + day + ' ' + hour + ':'
                + minute;
          }
          var res = {
            date: resultDate,
            format_date: resultFormatDate,
          };
          if (closedHandler != null) {
            closedHandler(calendar, res);
          }
          Picker.dismiss();
        },
      },
    });
    return calendarObj;
  },
  /**
   * 初始化赋值选择年月日时分控件的初始值
   * @param calendar
   * @param currentDate
   * @returns {string} 初始化值 yyyy-mm-dd hh:mm 格式的 String 字符串
   */
  initDateTimeInPicker: function (calendar, currentDate) {
    let _this = this;
    var initValArr = [];
    if (currentDate != undefined && currentDate != '' && currentDate != null) {
      var initDate = new Date(currentDate);
      var year = initDate.getFullYear();
      var month = initDate.getMonth();
      var today = initDate.getDate();
      var hour = initDate.getHours();
      var minute = initDate.getMinutes();
      initValArr = [year, month, today, hour, minute];
    } else {
      var nowDateObj = new Date();
      var nowToday = nowDateObj.getDate();
      var afterDateObj = new Date(nowDateObj.getTime() + 2 * 60 * 60 * 1000);
      var year = afterDateObj.getFullYear();
      var month = afterDateObj.getMonth();
      var today = afterDateObj.getDate();
      var hour = afterDateObj.getHours();
      if (nowToday == today) {
        if (hour <= 8) {
          hour = 10;
        } else if (hour > 20) {
          var newDateObj = new Date(afterDateObj.getTime() + 24 * 60 * 60
              * 1000);
          year = newDateObj.getFullYear();
          month = newDateObj.getMonth();
          today = newDateObj.getDate();
          hour = 10;
        }
      } else {
        hour = 10;
      }
      hour = _this.fix(hour, 2);
      initValArr = [year, month, today, hour, '00'];
    }
    calendar.setValue(initValArr);

    var yearStr = _this.fix(initValArr[0], 4);
    var monthStr = _this.fix(parseInt(initValArr[1]) + 1, 2);
    var dayStr = _this.fix(initValArr[2], 2);
    var hourStr = _this.fix(initValArr[3], 2);
    var minuteStr = _this.fix(initValArr[4], 2);
    return yearStr + '-' + monthStr + '-' + dayStr + ' ' + hourStr + ':'
        + minuteStr;
  },
  /**
   * 选择时间段控件
   * @param context
   * @param params
   * @returns {*}
   */
  selectDateRangeInMondal: function (context, params) {
    const _this = this;
    var inputElement = params.inputEl ? params.inputEl : '';
    var currentDateFromStr = params.currentDateFrom ? params.currentDateFrom
        : '';
    var currentDateToStr = params.currentDateTo ? params.currentDateTo : '';
    var maxDate = params.maxDate ? params.maxDate : null;
    var minDate = params.minDate ? params.minDate : null;
    var openedHandler = params.opened ? params.opened : null;
    var closedHandler = params.closed ? params.closed : null;
    var dateFormat = params.dateFormat ? params.dateFormat : 'yyyy-mm-dd';

    var initValues = [];
    if (currentDateFromStr != '') {
      initValues.push(new Date(currentDateFromStr));
    }
    if (currentDateToStr != '') {
      initValues.push(new Date(currentDateToStr));
    }
    var calendarRange = context.$f7.calendar.create({
      inputEl: inputElement,
      rangePicker: true,
      openIn: 'customModal',
      header: true,
      footer: true,
      dateFormat: dateFormat,
      value: initValues,
      formatValue(values) {
        var val1 = _this.getFormatDateTime(values[0]).substr(0, 10);
        var val2 = '';
        if (values.length > 1) {
          val2 = _this.getFormatDateTime(values[1]).substr(0, 10);
        }
        return val1 + ((val2 != '') ? '至' + val2 : '');
      },
      headerPlaceholder: '选择时间',
      toolbarCloseText: '确定',
      monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月',
        '十一月', '十二月'],
      dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
      maxDate: maxDate,
      minDate: minDate,
      on: {
        opened: function (calendar) {
          if (openedHandler != null) {
            openedHandler(calendar);
          }
        },
        closed: function (calendar) {
          if (closedHandler != null) {
            closedHandler(calendar);
          } else {
            // var values = calendar.getValue();
            // var val1 = _this.getFormatDateTime(values[0]).substr(0, 10);
            // var val2 = '';
            // if (values.length > 1) {
            //   val2 = _this.getFormatDateTime(values[1]).substr(0, 10);
            // }
          }
        }
      },
    });
    return calendarRange;
  },
  /**
   * 根据时间段获取一定格式的值
   * @param _type 时间段类型：minute hour day month year
   * @param _period 时间段值
   * @returns {string}
   */
  taskTimeConversion(_type, _period) {
    let _this = this;
    // <1h : 显示分钟；<24h : 显示时分；>1day : 显示天小时；>2days : 显示天；>7days : 显示周；>30days : 显示月；>1year : 显示年；
    let res = '';
    let periodTimestamp = 0;
    switch (_type) {
      case 'minute':
        periodTimestamp = _period * 60 * 1000;
        break;
      case 'hour':
        periodTimestamp = _period * 60 * 60 * 1000;
        break;
      case 'day':
        periodTimestamp = _period * 24 * 60 * 60 * 1000;
        break;
      case 'month':
        // 一月按 30 天计算
        periodTimestamp = _period * 30 * 24 * 60 * 60 * 1000;
        break;
      case 'year':
        periodTimestamp = _period * 365 * 24 * 60 * 60 * 1000;
        break;
    }

    let years = Math.floor(periodTimestamp / (365 * 24 * 60 * 60 * 1000));
    // 计算出月数
    // 计算年数后剩余的毫秒数
    let leave_year = periodTimestamp % (365 * 24 * 60 * 60 * 1000);
    let months = Math.floor(leave_year / (30 * 24 * 60 * 60 * 1000));
    // 计算出天数
    let leave_month = leave_year % (30 * 24 * 60 * 60 * 1000);
    let days = Math.floor(leave_month / (24 * 3600 * 1000));//计算出相差天数
    //计算出小时数
    let leave1 = leave_month % (24 * 3600 * 1000);    //计算天数后剩余的毫秒数
    let hours = Math.floor(leave1 / (3600 * 1000));
    //计算相差分钟数
    let leave2 = leave1 % (3600 * 1000);        //计算小时数后剩余的毫秒数
    let minutes = Math.floor(leave2 / (60 * 1000));
    //计算相差秒数
    let leave3 = leave2 % (60 * 1000);      //计算分钟数后剩余的毫秒数
    let seconds = Math.round(leave3 / 1000);
    // let retDate = years + ' 年 ' + months + ' 月 ' + days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒";

    if (years > 0) {
      res = years + ' 年';
    } else {
      if (months > 0) {
        res = months + ' 月';
      } else {
        if (days > 0) {
          if (days >= 7) {
            res = Math.floor(days / 7) + ' 周';
          } else if (days < 7 && days >= 2) {
            res = days + ' 天';
          } else {
            res = days + ' 天' + ((hours > 0) ? ' ' + hours + ' 小时' : '');
          }
        } else {
          if (hours > 0) {
            res = hours + ' 小时' + ((minutes > 0) ? ' ' + minutes + ' 分钟' : '');
          } else {
            res = ((minutes > 0) ? minutes + ' 分钟' : '0 分钟');
          }
        }
      }
    }

    return res;
  },
}